﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using log4net;
namespace QuanLyNhaSach
{
    public partial class frmDanhMucHangHoa : Form
    {
        //Tao Parttern Seleton
        static frmDanhMucHangHoa doituong = null;
        sv_QuanLyNhaSach.AdapterClient service = new sv_QuanLyNhaSach.AdapterClient(); public bool them;
        private ILog logger;
        public bool sua;
        public bool xoa;
        public bool trangthai = false;
        int add = 0, update = 0, delete = 0;
        public static frmDanhMucHangHoa TaoDoiTuong()
        {
            if (doituong == null)
                doituong = new frmDanhMucHangHoa();
            return doituong;
        }

        public frmDanhMucHangHoa()
        {
            InitializeComponent();
        }

        private void bt_them_Click(object sender, EventArgs e)
        {
            if (Application.OpenForms["FormPhu_MatHang"] == null) 
            {
                FormPhu_MatHang f = new FormPhu_MatHang();
                f.Name = "FormPhu_MatHang";
                if (f.ShowDialog()== System.Windows.Forms.DialogResult.Cancel)
                {
                    Load_MatHang();
                } 
            }
        }

        private void frmDanhMucHangHoa_Load(object sender, EventArgs e)
        {
            logger = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
            try
            {
                pictureBox1.Image = Image.FromStream(new MemoryStream(service.Layhinh(gv_danhmuchanghoa.CurrentRow.Cells["MaMH"].Value.ToString())));
                Load_MatHang();  
            }
            catch (Exception ex)
            {
                logger.Error("Error: " + ex.Message);
            }
         
        }
        private void Load_MatHang()
        {            
            DataSet ds = new DataSet();
            ds.ReadXml(new StringReader(service.LayDS_MatHang()));  
            try
            {
                if (ds.Tables.Count > 0)
                {
                    ds.Tables[0].Columns.Add("stt", typeof(int));
                    DataRow dr;
                    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                    {
                        dr = ds.Tables[0].NewRow();
                        dr["stt"] = i + 1;
                        ds.Tables[0].Rows[i]["stt"] = dr["stt"].ToString();
                        if (ds.Tables[0].Rows[i]["DonGiaNhap"].ToString().Contains(".0000") == true)
                        {
                            ds.Tables[0].Rows[i]["DonGiaNhap"] = ds.Tables[0].Rows[i]["DonGiaNhap"].ToString().Replace(".0000", "");
                        }
                        if (ds.Tables[0].Rows[i]["DonGiaSi"].ToString().Contains(".0000") == true)
                        {
                            ds.Tables[0].Rows[i]["DonGiaSi"] = ds.Tables[0].Rows[i]["DonGiaSi"].ToString().Replace(".0000", "");
                        }
                        if (ds.Tables[0].Rows[i]["DonGiaLe"].ToString().Contains(".0000") == true)
                        {
                            ds.Tables[0].Rows[i]["DonGiaLe"] = ds.Tables[0].Rows[i]["DonGiaLe"].ToString().Replace(".0000", "");
                        }                    
                    }
                    gv_danhmuchanghoa.DataSource = ds.Tables[0];
                }
                else
                    gv_danhmuchanghoa.DataSource = null;
            }
            catch (Exception ex)
            {
                logger.Error("Error: " + ex.Message);
            }
          

        }

        private void bt_sua_Click(object sender, EventArgs e)
        {
            try
            {
                if (trangthai == false)
                {
                    if (them == true && xoa == true && sua == true)
                    {
                        bt_them.Enabled = false;
                        bt_xoa.Enabled = false;
                    }
                    else
                    {
                        if (them == false)
                            bt_them.Enabled = false;
                        else
                            bt_them.Enabled = false;

                        if (xoa == false)
                            bt_xoa.Enabled = false;
                        else
                            bt_xoa.Enabled = true;
                    }
                    // giao dien
                    gv_danhmuchanghoa.AllowEditRow = true;
                    bt_sua.Image = global::QuanLyNhaSach.Properties.Resources.save;
                    bt_sua.Text = "Lưu";
                    trangthai = true;
                }
                else if (trangthai == true)
                {
                    string mamh = gv_danhmuchanghoa.CurrentRow.Cells["mamh"].Value.ToString();
                    string maloai = gv_danhmuchanghoa.CurrentRow.Cells["maloai"].Value.ToString();
                    string tenmh = gv_danhmuchanghoa.CurrentRow.Cells["tenmh"].Value.ToString();
                    double gianhap = double.Parse(gv_danhmuchanghoa.CurrentRow.Cells["dongianhap"].Value.ToString());
                    double giasi = double.Parse(gv_danhmuchanghoa.CurrentRow.Cells["dongiasi"].Value.ToString());
                    double giale = double.Parse(gv_danhmuchanghoa.CurrentRow.Cells["dongiale"].Value.ToString());
                    float km = float.Parse(gv_danhmuchanghoa.CurrentRow.Cells["khuyenmai"].Value.ToString());
                    string nhasx = gv_danhmuchanghoa.CurrentRow.Cells["nhasx"].Value.ToString();
                    string tacgia = gv_danhmuchanghoa.CurrentRow.Cells["tacgia"].Value.ToString();
                    string namxb = gv_danhmuchanghoa.CurrentRow.Cells["namsanxuat"].Value.ToString();
                    string dausach = gv_danhmuchanghoa.CurrentRow.Cells["dausach"].Value.ToString();
                    string dvt = gv_danhmuchanghoa.CurrentRow.Cells["dvt"].Value.ToString();
                    byte[] hinh = System.IO.File.ReadAllBytes(openFileDialog1.FileName);
                    int soluongton = int.Parse( gv_danhmuchanghoa.CurrentRow.Cells["soluongdauky"].Value.ToString());
                    int soluonghienco = int.Parse( gv_danhmuchanghoa.CurrentRow.Cells["soluonghienco"].Value.ToString());
                    if (!service.UpdateMatHang(mamh, maloai, tenmh, gianhap, giasi, giale, km, nhasx, tacgia, namxb, dausach, dvt, soluongton,soluonghienco, hinh))
                    {
                        MessageBox.Show("Sửa thất bại", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return;
                    }
                    else
                    {
                        update++;
                        MessageBox.Show("Sửa thành công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        Load_MatHang();
                        // giao diện
                        bt_sua.Image = global::QuanLyNhaSach.Properties.Resources.repair_new;
                        bt_sua.Text = "Sửa";
                        trangthai = false;
                        gv_danhmuchanghoa.AllowEditRow = false;
                    }
                    
                }
            }
            catch (Exception ex)
            {
                logger.Error("Error: " + ex.Message);
            }
            

        }

        private void bt_xoa_Click(object sender, EventArgs e)
        {
            try
            {
                if (MessageBox.Show("Bạn có thật sự muốn xóa mặt hàng này?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    string mamh = gv_danhmuchanghoa.CurrentRow.Cells[1].Value.ToString();
                    if (!service.DeleteMatHang(mamh))
                    {
                        MessageBox.Show("Xoá thất bại", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return;
                    }
                    else
                        MessageBox.Show("Xoá thành công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    Load_MatHang();
                }
            }
            catch (Exception ex)
            {
                logger.Error("Error: " + ex.Message);
            }
        }

        private void gv_danhmuchanghoa_RowsChanged(object sender, Telerik.WinControls.UI.GridViewCollectionChangedEventArgs e)
        {
            try
            {
                DataSet ds = new DataSet();
                ds.ReadXml(service.selectqueryByText("select hinh from mathang"));
            }
            catch (Exception ex)
            {
                logger.Error("Error: " + ex.Message);
            }

        }

        private void bt_timhinh_Click(object sender, EventArgs e)
        {
            try
            {
                openFileDialog1.Filter = "JPG|*.jpg|GIF|*.gif";
                openFileDialog1.ShowDialog();
                pictureBox1.ImageLocation = openFileDialog1.FileName;
            }
            catch (Exception ex)
            {
                logger.Error("Error: " + ex.Message);
            }
        }

        private void gv_danhmuchanghoa_CurrentRowChanged(object sender, Telerik.WinControls.UI.CurrentRowChangedEventArgs e)
        {
            try
            {
                pictureBox1.Image = Image.FromStream(new MemoryStream(service.Layhinh(e.CurrentRow.Cells["mamh"].Value.ToString())));
            }
            catch (Exception ex)
            {
                logger.Error("Error: " + ex.Message);
            }
        }

    }
}
